// 这是pinia的UI管理
import { defineStore } from 'pinia'
import { ref, computed } from 'vue'

// ui 的store
export const useUIStore = defineStore('UI', () => {
  const isCollapsed = ref(false)
  const getAvatarUrl = (name) => {
    return new URL(`../assets/img/${name}.jpg`, import.meta.url).href
  }

  // 页面加载时间（登录时间）
  const pageLoadTime = ref(new Date())

  // 计算属性：格式化的登录时间
  const getLoginTime = computed(() => {
    const now = pageLoadTime.value
    const year = now.getFullYear()
    const month = String(now.getMonth() + 1).padStart(2, '0')
    const day = String(now.getDate()).padStart(2, '0')
    const hours = String(now.getHours()).padStart(2, '0')
    const minutes = String(now.getMinutes()).padStart(2, '0')
    const seconds = String(now.getSeconds()).padStart(2, '0')
    return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`
  })

  // 城市信息状态（全局只获取一次）
  const city = ref('')
  const isCityFetched = ref(false)

  return {
    isCollapsed
    , getAvatarUrl
    , getLoginTime
    , city
    , isCityFetched
  }
})
